import { inject, injectable } from 'inversify';
import { PermissionStorage, PermissionState } from '@2/app/core/storage/permission.storage';
import { UserStorage } from '@2/app/core/storage/user.storage';
import { TYPES } from '@2/app/core/ioc/types';
import { Store } from 'pinia';
import { permissionDefineStore } from '@2/app/core/storage/define/permission.define';
import { Menu } from '@/router/types';

/**
 * @program: microservice-central-portal
 * @description: 许可授权存储接口实现
 * @author: baiHoo.chen
 * @create: 2022年5月26日
 **/
@injectable()
export class PermissionStorageImpl implements PermissionStorage {
  private readonly permissionStore: Store<string, PermissionState>;

  constructor(@inject(TYPES.UserStorage) protected userStorage: UserStorage) {
    this.permissionStore = permissionDefineStore();
  }
  /**
   * 销毁状态信息
   */
  destroy(): void {
    this.permissionStore.dynamicAddedRoute = false;
    this.permissionStore.authorities = [];
    this.permissionStore.backMenuList = [];
    this.permissionStore.lastBuildMenuTime = 0;
  }

  getAuthorities(): string[] {
    return this.permissionStore.authorities;
  }

  getBackMenuList(): Menu[] {
    return this.permissionStore.backMenuList;
  }

  getFrontMenuList(): Menu[] {
    return this.permissionStore.frontMenuList;
  }

  getLastBuildMenuTime(): number {
    return this.permissionStore.lastBuildMenuTime;
  }

  isDynamicAddedRoute(): boolean {
    return this.permissionStore.dynamicAddedRoute;
  }

  setAuthorities(authorities: string[]) {
    this.permissionStore.authorities = authorities;
  }

  setBackMenuList(menuList: Menu[]) {
    this.permissionStore.backMenuList = menuList;
    menuList?.length > 0 && this.setLastBuildMenuTime();
  }

  setDynamicAddedRoute(added: boolean) {
    this.permissionStore.dynamicAddedRoute = added;
  }

  setFrontMenuList(menuList: Menu[]) {
    this.permissionStore.frontMenuList = menuList;
  }

  setLastBuildMenuTime() {
    this.permissionStore.lastBuildMenuTime = new Date().getTime();
  }
  /**
   * 返回需要使用的 定义存储对象{@see permissionDefineStore}
   */
  useDefineStore(): Store<string, PermissionState> {
    return this.permissionStore;
  }
}
